package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.common.type.EnumErrorCode;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.domain.vo.JazCoursesChapterAddVo;
import com.jinzhi.jzweb.domain.vo.JazCoursesTeacherVo;
import com.jinzhi.jzweb.domain.vo.JazCoursesVo;

import com.jinzhi.jzweb.service.JazCoursesChapterService;
import com.jinzhi.jzweb.service.JazCoursesTeacherService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.service.JazCoursesService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 课程表
 * </pre>
 * <small> 2024-05-14 15:43:34 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazCourses")
public class JazCoursesController extends BaseController {
	@Autowired
	private JazCoursesService jazCoursesService;
	@Autowired
	private JazCoursesTeacherService jazCoursesTeacherService;
	@Autowired
	JazCoursesChapterService jazCoursesChapterService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazCourses:list")
	String JazCourses(){
	    return "jzweb/jazCourses/index";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazCourses:jazCourses")
	public Result<Page<JazCoursesDO>> list(JazCoursesDO jazCoursesDTO){
		// 查询列表数据
		Page<JazCoursesDO> page = jazCoursesService.selectPage(getPage(JazCoursesDO.class),
					jazCoursesService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				JazCoursesDO obj = (JazCoursesDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	/**
	 *  课程列表查询
	 * @return
	 */
	@GetMapping("/selCourseList")
	@ResponseBody
	public Result<Page<JazCoursesDO>> selCourseList(@RequestParam(value = "name",required = false) String name,
									  @RequestParam(value = "teacherName",required = false) String teacherName,
									  @RequestParam("current")int current,
									  @RequestParam("size")int size)  {

		return jazCoursesService.selCourseList(name,teacherName,current,size);
	}
	
	@GetMapping("/addInfo/{type}/{id}/{tableName}")
	String addInfo(@PathVariable("type") String type,@PathVariable("tableName") String tableName,@PathVariable("id") Long id,Model model){
		model.addAttribute("type", type);
		model.addAttribute("id", id);
		model.addAttribute("tableName", tableName);
		List lists=new ArrayList();
		if(type.equals("editZ")){
			lists.add(jazCoursesChapterService.selectById(id));
		}
		if(type.equals("editJ")){
			lists.add(jazCoursesChapterService.selectById(id));
		}
//		JazCoursesChapterAddVo

		if(null==lists||lists.size()==0){
			lists.add(new JazCoursesChapterDO());
		}

		model.addAttribute("voList",lists);
		return "jzweb/jazCourses/addInfo";
	}
	@GetMapping("/add/{type}/{id}")
	String add(@PathVariable("type") String type,@PathVariable("id") Long id,Model model){
		model.addAttribute("type", type);
		JazCoursesDO jazCoursesDO=jazCoursesService.selectById(id);
		model.addAttribute("myData", jazCoursesDO);
		model.addAttribute("userId", id);
		List<JazCoursesTeacherDO> lists=jazCoursesTeacherService.selectList(new EntityWrapper<JazCoursesTeacherDO>().eq("courses_id", id));
		if(null==lists||lists.size()==0){
			lists.add(new JazCoursesTeacherDO());
		}
		String images="";
		String detailImg="";
		if(null!=jazCoursesDO && null!=jazCoursesDO.getId()){
			images=jazCoursesDO.getImgUrl()==null?"":jazCoursesDO.getImgUrl();
		}
		if(null!=jazCoursesDO && null!=jazCoursesDO.getId()){
			detailImg=jazCoursesDO.getDetailImg()==null?"":jazCoursesDO.getDetailImg();
		}
		model.addAttribute("images","".equals(images)?new String[0]:images.split(","));
		model.addAttribute("detailImg","".equals(detailImg)?new String[0]:detailImg.split(","));
		model.addAttribute("voList",lists);


	    return "jzweb/jazCourses/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazCourses:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazCoursesDO jazCourses = jazCoursesService.selectById(id);
		model.addAttribute("jazCourses", jazCourses);
	    return "jzweb/jazCourses/edit";
	}
	

//	@ResponseBody
//	@PostMapping("/save")
//	@RequiresPermissions("jzweb:jazCourses:add")
//	public Result<String> save( JazCoursesDO jazCourses){
//		jazCoursesService.insert(jazCourses);
//        return Result.ok();
//	}

	@Log(value = "添加课程",sysModule = 6,operationType = 1)
	@ResponseBody
	@PostMapping("/saveCourse")
	public Result<String> saveCourse(@RequestBody JazCoursesVo jazCoursesVo){
		jazCoursesService.saveCourse(jazCoursesVo);
		return Result.ok();
	}

	@Log(value = "修改课程表" ,sysModule = 6,operationType = 3)
	@ResponseBody
	@PostMapping("/updateCourse")
	public Result<String> updateCourse(@RequestBody JazCoursesVo jazCoursesVo){
		jazCoursesService.updateCourse(jazCoursesVo);
		return Result.ok();
	}
	/**
	 * 上下架
	 * @return
	 */
	@Log(value = "课程上下架",sysModule = 6,operationType = 3)
	@GetMapping("/editStatus")
	@ResponseBody
	public Result<String> editStatus(@RequestParam("id")Long id){
		JazCoursesDO jazCoursesDO = jazCoursesService.selectById(id);
		if (jazCoursesDO.getState().equals(1)){
			jazCoursesDO.setState(0);
		}else {
			jazCoursesDO.setState(1);
		}
		jazCoursesService.updateById(jazCoursesDO);
		return Result.ok();
	}
	
//	@Log("修改课程表")
//	@ResponseBody
//	@RequestMapping("/update")
//	@RequiresPermissions("jzweb:jazCourses:edit")
//	public Result<String>  update( JazCoursesDO jazCourses){
//		boolean update = jazCoursesService.updateById(jazCourses);
//		return update ? Result.ok() : Result.fail();
//	}
	
	@Log(value = "删除课程",sysModule = 6,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Long id){
		List<JazCoursesChapterDO> doList = jazCoursesChapterService.selectList(new EntityWrapper<JazCoursesChapterDO>().eq("parent_id", id));
		if (!doList.isEmpty()){
			return Result.fail("该课程下有章节无法删除！");
		}
		jazCoursesService.deleteById(id);
        return Result.ok();
	}
	
	@Log(value = "批量删除课程",sysModule = 6,operationType = 2)
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazCourses:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		jazCoursesService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
